package doc.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author eollse-wj
 * @ClassName ResultVO
 * @date 2020/7/20
 * @description
 */
@Data
@ApiModel(description = "响应返回对象")
public class ResultVO<T> {

    /**
     *返回值
     */
    @ApiModelProperty(value = "返回值[1000-成功，1001-错误提示，1002-系统异常，401-无权限/未登录]", position = 1)
    private Integer code;
    /**
     * 返回值说明
     */
    @ApiModelProperty(value = "返回值说明", position = 2)
    private String msg;
    /**
     * 返回对象
     */
    @ApiModelProperty(value = "返回对象", position = 3)
    private T data;

    public ResultVO(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultVO(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResultVO ok() {
        return new ResultVO(Code.CODE_OK.getCode(), "成功");
    }

    public static ResultVO ok(Object data) {
        return new ResultVO(Code.CODE_OK.getCode(), "成功", data);
    }

    /**
     * 错误提示
     * @return
     */
    public static ResultVO error() {
        return new ResultVO(Code.CODE_ERROR.getCode(), "操作失败");
    }
    /**
     * 错误提示
     * @param msg
     * @return
     */
    public static ResultVO error(String msg) {
        return new ResultVO(Code.CODE_ERROR.getCode(), msg);
    }

    /**
     * 系统异常
     * @return
     */
    public static ResultVO exception() {
        return new ResultVO(Code.CODE_EXCEPTION.getCode(), "系统异常");
    }

    /**
     * 未登录
     * @return
     */
    public static ResultVO no_login() {
        return new ResultVO(Code.CODE_NO_LOGIN.getCode(), "未登录！");
    }

    /**
     * code
     */
    enum Code{
        CODE_OK(1000),
        CODE_ERROR(1001),
        CODE_EXCEPTION(1002),
        CODE_NO_LOGIN(401);
        private int code;

        Code(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }
}
